package com.htjc.integral.conversion.tokenUtil.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.htjc.integral.conversion.enums.Constant;
import com.htjc.integral.conversion.mapper.EcSystemParamMapper;
import com.htjc.integral.conversion.tokenUtil.service.IUSCClientService;
import com.htjc.integral.conversion.util.SgccMallUtil;
import com.sgcc.sip.access.rest.SipRestAccessAgent;
import com.sgcc.sip.access.rest.SipRestAccessAgentFactory;

@Service
public class USCClientService implements IUSCClientService{
	@Autowired
	private EcSystemParamMapper ecSystemParamMapper;
	
	public final static String TENANT="state_grid";
	public final static String MEMBER="esgcc_personal";
	
	private final static Log log = LogFactory.getLog(USCClientService.class);
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	public static String  url = ""; 
	public static String  pwd = ""; 
	public static String  secretKey = ""; 
	public static String  account = ""; 
	public static String  uscBaseUrl = "";
	private static SipRestAccessAgent agent = null;
	
	
	public String CheckTokenBackUserId(String token) {
		String info = "";
		try {
			SipRestAccessAgent agent=agent_black();
			Map<String,String> map=new HashMap<String,String>();
			map.put("tenant",Constant.TENANT_CODE_PARAM_VALUE);
			map.put("member",Constant.APP_CODE_PERSONAL);
			String uscInfo=objectMapper.writeValueAsString(map);
		    map.clear();
			map.put("uscInfo",uscInfo);
			map.put("token",token);
			String authUrl=uscBaseUrl+"/user/oauthquery";
			info=agent.post(authUrl,map);
		} catch (Exception e) {
			log.error("获取用户信息错",e);
			throw new RuntimeException(e);
		} 
		return info;
	}
	
/*	public String logout(String uscInfo_String, String quInfo_String) {
		String info = "";
		JSONObject quInfo = new JSONObject();
		JSONObject uscInfo_show = JSONObject.fromObject(uscInfo_String);
		JSONObject quInfo_show = JSONObject.fromObject(quInfo_String);
		try {
			SipRestAccessAgent agent=agent_black();
			Map<String,String> map=new HashMap<String,String>();
			map.put("tenant",uscInfo_show.getString("tenant"));
			map.put("member",uscInfo_show.getString("member"));
			String uscInfo = objectMapper.writeValueAsString(map);
			map.clear();
			quInfo.put("userId", quInfo_show.getString("userId"));
			quInfo.put("token", quInfo_show.getString("token"));
			
			map.put("quInfo",quInfo.toString());
			map.put("uscInfo",uscInfo);
			String authUrl=uscBaseUrl+"/user/loginout";
			info=agent.post(authUrl,map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} 
		return info;
	}*/
	
	
	public SipRestAccessAgent agent_black(){
		if( agent==null ) {
//			url = SgccMallUtil.getInstance().getConfValue("site.sip.H5.url");
//			account = SgccMallUtil.getInstance().getConfValue("site.sip.H5.account");
//			pwd = SgccMallUtil.getInstance().getConfValue("site.sip.H5.pwd");
//			secretKey = SgccMallUtil.getInstance().getConfValue("site.sip.H5.secretKey");
//			uscBaseUrl = SgccMallUtil.getInstance().getConfValue("site.sip.H5.uscBaseUrl");
			
			url = ecSystemParamMapper.getEcSystemparam("site.sip.H5.url").getParamvalue();
			account = ecSystemParamMapper.getEcSystemparam("site.sip.H5.account").getParamvalue();
			pwd = ecSystemParamMapper.getEcSystemparam("site.sip.H5.pwd").getParamvalue();
			secretKey = ecSystemParamMapper.getEcSystemparam("site.sip.H5.secretKey").getParamvalue();
			uscBaseUrl = ecSystemParamMapper.getEcSystemparam("site.sip.H5.uscBaseUrl").getParamvalue();
			
			SipRestAccessAgentFactory factory = new SipRestAccessAgentFactory(url,account,pwd,secretKey);
			agent=factory.getInstance();
		}
		return agent;
	}
	
}
